package com.eqdd.yiqidian.utils;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;

import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.loading.wheelprogressdialog.WheelProgressHelper;
import com.eqdd.yiqidian.bean.rebuild.GroupListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.ui.yiqiyue.AddArticleActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.io.File;
import java.util.List;

import io.rong.imkit.RongIM;
import io.rong.imlib.model.Group;

/**
 * Created by 吕志豪 on 17-10-10  上午11:47.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class HttpUtil {
    public static void refreshGroup() {
        OkGo.<HttpResult<List<GroupListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.GROUP_LIST_INFO)
                .params("userGuid", GreenDaoUtil.getUser().getGuid())
                .execute(new JsonCallBack<HttpResult<List<GroupListResultBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpResult<List<GroupListResultBean>>> response) {
                        HttpResult<List<GroupListResultBean>> httpResult = response.body();
                        if (httpResult.getStatus() == 200) {
                            GreenDaoUtil.deleteAllGroup();
                            for (GroupListResultBean groupBean : httpResult.getItems()) {
                                GreenDaoUtil.insertGroup(groupBean);
                                Group group = new Group(groupBean.getGroupid(), TextUtils.isEmpty(groupBean.getGroupname()) ? "未命名" : groupBean.getGroupname(), Uri.parse(groupBean.getGroupphoto()));
                                RongIM.getInstance().refreshGroupInfoCache(group);
                            }
                        }
                    }
                });
    }

    public static void upLoadPic(Context context, String filePath, PicBack picBack) {

        WheelProgressHelper wheelProgressHelper = new WheelProgressHelper(context);
        wheelProgressHelper.showLoading("图片上传中");
        OkGo.<HttpResult<String>>post(HttpConfig.BASE_URL + HttpConfig.UPLOAD_IMG)
                .params("image", new File(filePath))
                .execute(new JsonCallBack<HttpResult<String>>() {
                    @Override
                    public void onSuccess(Response<HttpResult<String>> response) {
                        HttpResult<String> httpResult = response.body();
                        if (httpResult.getStatus() == 200) {
                            wheelProgressHelper.hideLoading("上传成功");
                            picBack.onPicBack(httpResult.getItems());
                        } else {
                            wheelProgressHelper.hideLoading("上传失败");
                        }
                    }

                    @Override
                    public void onError(Response<HttpResult<String>> response) {
                        super.onError(response);
                        wheelProgressHelper.hideLoading("上传失败");

                    }
                });

    }

    public static void addArticleMenu(BaseActivity baseActivity, String content, ResultBack resultBack) {
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.YUE_ARTICLE_ADD_MENU)
                .params("userGuid", AppConstent.user.getGuid())
                .params("menuName", content)
                .execute(new DialogCallBack<HttpResult>(baseActivity) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            resultBack.onResultBack();
                        }
                    }
                });
    }

    public static void updateArticleMenuName(BaseActivity baseActivity, int menuId, String content, ResultBack resultBack) {
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.YUE_UPDATE_MENU_NAME)
                .params("userGuid", AppConstent.user.getGuid())
                .params("articleMenuId", menuId)
                .params("articleMenuName", content)
                .execute(new DialogCallBack<HttpResult>(baseActivity) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            resultBack.onResultBack();
                        }
                    }
                });
    }

    public static void deleteArticleMenu(BaseActivity baseActivity, int articleId, ResultBack resultBack) {
        OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.YUE_DELETE_ARTICLE_MENU)
                .params("userGuid", AppConstent.user.getGuid())
                .params("articleMenuId", articleId)
                .execute(new DialogCallBack<HttpResult>(baseActivity) {
                    @Override
                    public void onSuccess(Response<HttpResult> response) {
                        HttpResult httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            resultBack.onResultBack();
                        }
                    }
                });
    }

    public interface PicBack {
        void onPicBack(String url);
    }

    public interface ResultBack {
        void onResultBack();
    }

}
